﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace EkStudio.com.bean.xml.LoginConfig
{
    [XmlRoot("Root")]
    public class WebLoginCfg
    {
        private List<WebLoginItem> _item = null;

        [XmlArray]
        [XmlArrayItem("LoginItem")]
        public List<WebLoginItem> Item
        {
            get
            {
                return _item;
            }

            set
            {
                _item = value;
            }
        }

        public void AddItem(int typeIndex,String url,String user,String pwd,bool Checked)
        {
            if (_item == null) _item = new List<WebLoginItem>();
            WebLoginItem old = null;
            //修改以前列表为非默认
            foreach (WebLoginItem oit in _item)
            {
                if(oit.LoginUrl == url)
                {
                    old = oit;
                    
                }
                oit.LoginDefault = false;
            }
            if (old != null)
            {
                old.LoginType = typeIndex;
                old.LoginUrl = url;
                old.LoginUser = user;
                old.LoginPwd = pwd;
                old.LoginDefault = Checked;
                old = null;
            }
            else
            {
                WebLoginItem it = new WebLoginItem();
                it.LoginType = typeIndex;
                it.LoginUrl = url;
                it.LoginUser = user;
                it.LoginPwd = pwd;
                it.LoginDefault = Checked;
                _item.Add(it);
            }
           
        }

        public void RemoveItem(String url)
        {
            if (_item != null)
            {
                foreach(WebLoginItem it in _item)
                {
                    if(it.LoginUrl == url)
                    {
                        _item.Remove(it);
                        break;
                    }
                }
            }
        }
    }
}
